package org.zp.handler;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.AddNetwork;
import org.zp.dto.Paging;
import org.zp.pojo.AdminInfo;
import org.zp.service.AdminLogInfoService;
import org.zp.service.NetWorkInfoService;
import org.zp.util.Ajax;
import org.zp.util.ValiAdmindate;
import org.zp.vo.NetWorkVo;

import com.github.pagehelper.PageInfo;

@RestController
public class NetWorkHandler {
@Resource
private NetWorkInfoService n;
@Resource
private AdminLogInfoService log;

private String url = "网点列表";
@GetMapping("selectNetWorkINFO.act")
public void selectNetWorkINFO(String name,String fname,Paging dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer startpagee = Integer.parseInt(dto.getStartpage());
	Integer limitpagee = Integer.parseInt(dto.getLimitpage());
	if (name.equals("")) {
		name = null;
	}
	if (fname.equals("")) {
		fname = null;
	}
	List<NetWorkVo> list = n.selectNetWorkINFO(startpagee, limitpagee,name,fname);
	PageInfo<NetWorkVo> pageInfo = new PageInfo<NetWorkVo>(list);
	Integer count = (int) pageInfo.getTotal();

	System.out.println("管理员管控多条件查询count:" + count);
	Ajax.success(resp, "list", "count", list, count);
}
@PostMapping("netWorkNameEdit.act")
public void netWorkNameEdit(String networkName,String neId,HttpServletRequest req, HttpServletResponse resp) {
	Integer neIdd = Integer.parseInt(neId);
	Integer index = n.netWorkNameEdit(neIdd,networkName);
	if (index > 0) {
		Ajax.success(resp);
		System.out.println("修改成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	} else {
		Ajax.error(resp, "修改失败", 10001);
		System.out.println("修改失败");
	}
}
@PostMapping("netWorkDel.act")
public void netWorkDel(String neId,HttpServletRequest req, HttpServletResponse resp) {
	Integer neIdd = Integer.parseInt(neId);
	Integer index = n.netWorkDel(neIdd);
	if (index > 0) {
		Ajax.success(resp);
		System.out.println("删除成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 2;//此为删除的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	} else {
		Ajax.error(resp, "删除失败", 10001);
		System.out.println("删除失败");
	}
}
@PostMapping("addNetwork.act")
public void addNetwork(AddNetwork dto,HttpServletRequest req, HttpServletResponse resp) {
	
	String netname = dto.getNetName();
	String detail = dto.getDetail();
	
	Integer one = 0;
	Integer tow = 0;
	Integer three = 0;
	try {
		one = Integer.parseInt(dto.getAddressOne());
		tow = Integer.parseInt(dto.getAddressTwo());
		three = Integer.parseInt(dto.getAddressThree());
	} catch (Exception e) {
		System.out.println("转换失败");
	}
	if (one==0||tow==0||three==0) {
		Ajax.error(resp, "请补充完整地址信息");
		return;
	}
	ValiAdmindate validate = new ValiAdmindate();
	if (!validate.checkContent(netname)) {
		Ajax.error(resp, validate.getError(), 10000);
		return;
	}
	if (!validate.checkContent(detail)) {
		Ajax.error(resp, validate.getError(), 10000);
		return;
	}
	Integer index = n.addNetwork(three,netname,detail);
	if (index > 0) {
		Ajax.success(resp);
		System.out.println("添加成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 1;//此为增的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	} else {
		Ajax.error(resp, "添加失败", 10001);
		System.out.println("添加失败");
	}
	
}
}
